package com.bdqn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bdqn.pojo.Book;

@WebServlet(urlPatterns={"/showBook"},name="bookServlet")
public class BookServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BookServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		Book book1 = new Book();
		book1.setId(1);
		book1.setName("java");
		book1.setPrice(100.5);
		book1.setGender("1");
		
		Book book2 = new Book();
		book2.setId(2);
		book2.setName("php");
		book2.setPrice(300.5);
		book2.setGender("0");
		
		Book book3 = new Book();
		book3.setId(3);
		book3.setName("python");
		book3.setPrice(200.5);
		book3.setGender("1");
		
		List<Book> list = new ArrayList<Book>();
		list.add(book1);
		list.add(book2);
		list.add(book3);
		//创建map 为了转换layUI要求的格式：如下格式
		/*{"code":0,"msg":"","count":1000,
			"data":[{"id":1,"name":"java","price":100.5},
					{"id":2,"name":"php","price":300.5},
					{"id":3,"name":"python","price":200.5}]}
		*/
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code",0);
		map.put("msg","");
		//应该调用方法查询后台真实的数据条数，此处3 是随意写的
		map.put("count",3);
		map.put("data",list);
		String json = JSON.toJSONString(map);
		StringBuilder sbf = new StringBuilder(json);
		
		//转换标准格式
		sbf = sbf.replace(0, 1, "{\"code\":0,\"msg\":\"\",");
		System.out.println(sbf.toString());
		
		out.println(sbf.toString());
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
